package org.jeecg.modules.nikeProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.nikeProject.entity.SportsObservationThinking;
import org.jeecg.modules.nikeProject.entity.SportsProject;
import org.jeecg.modules.nikeProject.entity.SportsReference;
import org.jeecg.modules.nikeProject.mapper.SportsObservationThinkingMapper;
import org.jeecg.modules.nikeProject.mapper.SportsProjectMapper;
import org.jeecg.modules.nikeProject.service.ISportsObservationThinkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * @Description: 观察与思考
 * @Author: jeecg-boot
 * @Date:   2024-12-11
 * @Version: V1.0
 */
@Service
public class SportsObservationThinkingServiceImpl extends ServiceImpl<SportsObservationThinkingMapper, SportsObservationThinking> implements ISportsObservationThinkingService {

    @Autowired
    private SportsProjectMapper sportsProjectMapper;

    @Autowired
    private SportsObservationThinkingMapper sportsObservationThinkingMapper;

    @Override
    public Result<?> add(String username, SportsObservationThinking sportsObservationThinking) {
        if ("".equals(sportsObservationThinking.getDocumentaryName()) && "".equals(sportsObservationThinking.getObservationAndThinking())){
            return Result.OK("未填写相关信息");
        }

        String projectId = sportsObservationThinking.getProjectId();
        if (null == projectId || "".equals(projectId)) {
            return Result.error("未查找到案例信息，请联系管理员处理！");
        }
        SportsProject sportsProject = sportsProjectMapper.selectById(projectId);
        if (CommonConstant.PROJECT_STEP_FINISH.equals(sportsProject.getProjectStep())){
            return Result.error("案例已提交，不允许编辑!");
        }
        //判断观察与思考是否存在
        SportsObservationThinking observationThinking = getObservationThinkingByProjectId(projectId);
        if (null != observationThinking){
            observationThinking.setUpdateTime(new Date());
            observationThinking.setUpdateBy(username);
            observationThinking.setIzActive("1");
            sportsObservationThinkingMapper.updateById(observationThinking);
        } else {
            sportsProject.setProjectStep(CommonConstant.PROJECT_STEP_OBSERVING_AND_THINKING);
            sportsProject.setUpdateBy(username);
            sportsProject.setUpdateTime(new Date());
            sportsProjectMapper.updateById(sportsProject);
        }

        int  i = sportsObservationThinkingMapper.insert(sportsObservationThinking);
        if (i == 0) {
            return Result.error("新增失败！");
        }
        return Result.OK("新增成功！");
    }

    public SportsObservationThinking getObservationThinkingByProjectId(String projectId) {
        LambdaQueryWrapper<SportsObservationThinking> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsObservationThinking::getIzActive, "0");
        lambdaQueryWrapper.eq(SportsObservationThinking::getProjectId, projectId);
        return sportsObservationThinkingMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public Result<?> update(String username, SportsObservationThinking sportsObservationThinking) {
        SportsObservationThinking thinking = sportsObservationThinkingMapper.selectById(sportsObservationThinking.getId());
        if (null != thinking) {
            sportsObservationThinking.setUpdateBy(username);
            sportsObservationThinking.setUpdateTime(new Date());
            sportsObservationThinkingMapper.updateById(sportsObservationThinking);
            return Result.OK("更新成功！");
        }
        return Result.error("当前案例对应的观察与思考不存在！");
    }
}
